class AIAssistant:
    def __init__(self, id, name, type, version, is_online, score):
        self.id = id
        self.name = name
        self.type = type
        self.version = version
        self.is_online = is_online
        self.score = score

    def __str__(self):
        status = "🟢在线" if self.is_online else "🔴离线"
        return f"ID: {self.id}, 名称: {self.name}, 类型: {self.type}, 版本: {self.version}, 状态: {status}, 能力评分: {self.score} [{self.type}]"


class AIManager:
    def __init__(self):
        self.assistants = []
        self.max_assistants = 100  # 假设最多存储100个助手信息

    def add_assistant(self, id, name, type, version, is_online, score):
        if len(self.assistants) >= self.max_assistants:
            print("助手数量已达上限，无法添加更多助手。")
            return False
        new_assistant = AIAssistant(id, name, type, version, is_online, score)
        self.assistants.append(new_assistant)
        print(f"助手【{name}】添加成功！")
        return True

    def list_assistants(self):
        for assistant in self.assistants:
            print(assistant)

    def get_assistant_by_id(self, id):
        for assistant in self.assistants:
            if assistant.id == id:
                print(f"查询结果: {assistant}")
                return assistant
        print(f"未找到ID为{id}的助手。")
        return None

    def update_assistant(self, id, **kwargs):
        for assistant in self.assistants:
            if assistant.id == id:
                for key, value in kwargs.items():
                    if key == 'name':
                        assistant.name = value
                    elif key == 'type':
                        assistant.type = value
                    elif key == 'version':
                        assistant.version = value
                    elif key == 'online':
                        assistant.is_online = value
                    elif key == 'score':
                        assistant.score = value
                print(f" 助手【{id}】修改成功！")
                return True
        print(f" 未找到ID为{id}的助手，修改失败。")
        return False

    def delete_assistant(self, id):
        for i, assistant in enumerate(self.assistants):
            if assistant.id == id:
                del self.assistants[i]
                print(f" 助手【{assistant.name}】已删除。")
                return True
        print(f"未找到ID为{id}的助手，删除失败。")
        return False

    def start_assistant_service(self, id):
        for assistant in self.assistants:
            if assistant.id == id:
                if not assistant.is_online:
                    assistant.is_online = True
                    print(f"助手【{assistant.name}】服务已启动。")
                else:
                    print(f"助手【{assistant.name}】已在运行。")
                return True
        print(f"未找到ID为{id}的助手，启动失败。")
        return False

    def stop_assistant_service(self, id):
        for assistant in self.assistants:
            if assistant.id == id:
                if assistant.is_online:
                    assistant.is_online = False
                    print(f"助手【{assistant.name}】服务已停止。")
                else:
                    print(f"助手【{assistant.name}】当前离线，无需停止。")
                return True
        print(f"未找到ID为{id}的助手，停止失败。")
        return False

    def assign_task(self, id, task):
        for assistant in self.assistants:
            if assistant.id == id:
                if assistant.is_online:
                    print(f"任务已分配给【{assistant.name}】: {task}")
                    # 这里可以添加实际的任务处理逻辑
                else:
                    print(f"{assistant.name} 当前离线，无法执行任务。")
                return True
        print(f"未找到ID为{id}的助手，任务分配失败。")
        return False

    def show_statistics(self):
        total_assistants = len(self.assistants)
        online_assistants = sum(1 for a in self.assistants if a.is_online)
        offline_assistants = total_assistants - online_assistants
        avg_score = sum(a.score for a in self.assistants) / total_assistants if total_assistants > 0 else 0
        print(f"系统统计信息：\n总助手数: {total_assistants}\n在线助手数: {online_assistants}\n离线助手数: {offline_assistants}\n平均能力评分: {avg_score:.2f}")


def main():
    manager = AIManager()
    while True:
        print("\n========== AI助手管理器 ==========")
        print("1. 查看所有AI助手列表")
        print("2. 根据ID查询助手详情")
        print("3. 添加新的AI助手")
        print("4. 根据ID修改助手信息")
        print("5. 删除AI助手（按ID）")
        print("6. 启动AI助手服务")
        print("7. 停止AI助手服务")
        print("8. 为AI助手分配任务")
        print("9. 显示系统统计信息")
        print("0. 退出系统")
        print("===================================")
        choice = input("请选择操作: ")
        if choice == '1':
            manager.list_assistants()
        elif choice == '2':
            id = input("请输入要查询的AI助手ID: ")
            manager.get_assistant_by_id(id)
        elif choice == '3':
            id = input("请输入ID: ")
            name = input("请输入名称: ")
            type = input("请输入类型: ")
            version = input("请输入版本号: ")
            is_online = int(input("是否在线？(1-是, 0-否): ")) == 1
            score = int(input("请输入能力评分(0-100): "))
            manager.add_assistant(id, name, type, version, is_online, score)
        elif choice == '4':
            id = input("请输入ID: ")
            attrs = input("可修改属性列表为：name,type,version,online,score\n请输入要要修改的属性和值，用空格分割: ").split()
            attr_dict = {attrs[i]: (int(attrs[i + 1]) if attrs[i] in ['online', 'score'] else attrs[i + 1]) for i in
                         range(0, len(attrs), 2)}
            manager.update_assistant(id, **attr_dict)
        elif choice == '5':
            id = input("请输入要删除的AI助手ID: ")
            manager.delete_assistant(id)
        elif choice == '6':
            id = input("请输入要启动的AI助手ID: ")
            manager.start_assistant_service(id)
        elif choice == '7':
            id = input("请输入要停止的AI助手ID: ")
            manager.stop_assistant_service(id)
        elif choice == '8':
            id = input("请输入目标AI助手ID: ")
            task = input("请输入任务内容: ")
            manager.assign_task(id, task)
        elif choice == '9':
            manager.show_statistics()
        elif choice == '0':
            print("退出系统。")
            break
        else:
            print("无效的选择，请重新输入。")


if __name__ == "__main__":
    main()